<?php
    require_once dirname(__FILE__).'/../../class/rice.class.php';
    $cropID=$_GET["cropID"];
    $cropClass=new crop($cropID);
    $crop=$cropClass->getCrop();
    
    $riceObj=new rice($crop["rvID"]);
    $rice=$riceObj->getRice();
    $farmID=$crop["farmID"];
    
    $farmClass=new farm();
    $farmClass->setFarmID($farmID);
    $farm=$farmClass->getFarm();
    $memID=$farm["memID"];
    
    $memberClass=new member($memID);
    $member=$memberClass->getMember();
    
    $dateClass=new date();
    ?>
<style type="text/css">
    #structureContent{
        vertical-align: top;
    }
</style>
<div class="col-md-12" style="border-bottom: 1px solid #aaa;padding-top: 10px;padding-bottom: 10px;box-shadow: 0px 2px 5px #888;margin-bottom: 10px;">
    <?=$member["title"]." ".$member["firstName"]." ".$member["lastName"]?><br>
    <span style="font-size: 80%;">
        บันทึกต้นทุน แปลงนา <?php echo $farm["farmName"];?> <?=$farm["area"]?> ไร่
        <b><?=$rice["rvName"]?></b>
        <br>
        <?php
           $dateClass=new date();
           echo $dateClass->dateEncodeBE($crop["startDate"]);
           echo "-";
           echo $dateClass->dateEncodeBE($crop["endDate"]);
       ?>
    </span>
    <button class="btn btn-default" style="position: absolute;top: 0px; right: 0px;"
        onclick="
            window.open('?page=member/crop/addCropForm&farmID=<?=$farmID?>&cropID=<?=$cropID?>','_self');
        "
        >
        <span class="glyphicon glyphicon-pencil"></span> แก้ไข Crop
    </button>
</div>
<div style="display: inline-block;width: 100%;padding: 5px;">
    <div class="btn-group" style="float: right;">
        <button type="button" class="btn btn-default" style="float: left;"
            onclick="window.open('?page=member/crop/cropList&farmID=<?=$farmID?>','_self')"
            ><span class="glyphicon glyphicon-chevron-left"></span> เลือก Crop</button>
        <button type="button" class="btn btn-default active" 
            onclick="window.open('?page=member/crop/cropCostInput&cropID=<?=$_GET["cropID"]?>','_self')"
            >บันทึกต้นทุน</button>
        <button type="button" class="btn btn-default" 
            onclick="window.open('?page=member/crop/cropIncomeInput&cropID=<?=$_GET["cropID"]?>','_self')"
            >บันทึก รายรับ</button>
    </div>
</div>
<table style="width: 100%;">
    <tr>
        <td style="padding-top: 5px;padding-left: 10px;padding-right: 10px;">

            <form id="addCostForm" action="member/crop/cropCostSave.php" method="post" class="form-horizontal" target="tempFrame">
                <input type="hidden" name="cropID" value="<?=$cropID?>">
                <fieldset>
                    <?php
                        $queryCT="
                            select  *
                            from    cost_list
                            where   parent is null
                            ";
                        $resultCT=$mysqli->query($queryCT);
                        $iCT=1;
                        while($rowCT=$resultCT->fetch_array(MYSQLI_ASSOC)){
                            ?>
                    <!-- Appended Input-->
                    <div style="display: table;width: 100%;">
                        <div class="pc" style="float: left;width: 20%;"><?=$rowCT["costName"]?></div>
                        <div class="mobile" style="float: left;width: 100%;"><?=$rowCT["costName"]?></div>
                        <div class="mobile" style="float: left;width: 20%;">&nbsp;</div>
                        <div style="float: left;width: 60%;text-align: center;">
                            <span id="input<?=$rowCT["costID"]?>"><?=  number_format($cropClass->sumCost($rowCT["costID"]))?></span> บาท/ไร่
                        </div>
                        <div style="float: left;width: 20%;">
                            <button type="button" class='btn btn-default pull-right'
                            onclick="$('#moreCost<?=$rowCT["costID"]?>').slideToggle();"
                            >เพิ่มเติม <span class="glyphicon glyphicon-chevron-right"></span></button>
                        </div>
                    </div>
                    <script type="text/javascript">
                        $('#total<?=$rowCT[costID]?>').val($('#cost<?=$rowCT["costID"]?>').val()*<?=$farm["area"]?>);
                    </script>
                    
                    <div id='moreCost<?=$rowCT["costID"]?>' style="display: none;">
                        <?php
                            $queryMC="
                                select  *
                                from    cost_list
                                where   parent='".$rowCT["costID"]."'
                                ";
                            $resultMC=$mysqli->query($queryMC);
                            while($rowMC=$resultMC->fetch_array(MYSQLI_ASSOC)){
                                ?>
                        <div class="form-group">
                            <label class="col-md-4 control-label" for="appendedtext">

                            </label>
                            <div class="col-md-4">
                                <span style="font-size: 80%;white-space: pre"><?=$rowCT["costName"]?> / <?=$rowMC["costName"]?></span>
                                <div class="input-group">
                                    <input type="hidden" name="costID[]" value="<?=$rowMC["costID"]?>">
                                    <input id="cost<?=$rowMC[costID]?>" name="cost[]" class="form-control input<?=$rowCT["costID"]?>" placeholder="<?=$rowMC["costName"]?>" type="tel" 
                                        style="text-align: right;min-width: 100px;"
                                        value="<?=$cropClass->getCost($rowMC["costID"])?>"
                                        onkeyup="
                                            calculateSum('input<?=$rowCT["costID"]?>');
                                            var total = $(this).val()*<?=$farm["area"]?>;
                                            $('#total<?=$rowMC[costID]?>').val(total);
                                            
                                        "
                                        >
                                    <span class="input-group-addon">บาท/ไร่</span>
                                </div>
                                <button type="button" class="btn btn-default"
                                    onclick="
                                        $('#calDIV').show();
                                        $('#calDIV').css('top',$(this).offset().top);
                                        $('#calDIV').css('left',$(this).offset().left+$(this).width()+50);
                                        $('#calTo').val('cost<?=$rowMC[costID]?>');
                                        $('#calInput').val('');
                                    "
                                    >ช่วยคำนวณ</button>
                            </div>
                        </div>
                                    <?php
                                $iMC++;
                            }
                        ?>
                    </div>
                    <hr style="border-top: 1px solid #428BCA;">
                                <?php
                            $iCT++;
                        }
                    ?>
                </fieldset>
            </form>
        </td>
    </tr>
</table>
<iframe name="tempFrame" style="display: none;"></iframe>
<script type="text/javascript">
    function save(){
        $.post('member/crop/cropCostSave.php',
            $('#addCostForm').serialize()
        ,function(data){
            
        });
    }
    $('input').change(function(){
        save();
    });
</script>
<div id="calDIV" style="position: absolute;display: none;width: 200px;">
    <div class="input-group">
        <input type="hidden" id="calTo">
        <input id="calInput" name="cost[]" class="form-control" placeholder="กรอกต้นทุนรวม" type="tel" 
            style="text-align: right;min-width: 100px;"
            value="<?=$cropClass->getCost($rowMC["costID"])?>"
            onkeyup="
                var total = $(this).val()/<?=$farm["area"]?>;
                $('#'+$('#calTo').val()).val(total);
            "
            onblur="save();$('#calDIV').fadeOut()"
            >
        <!--<span class="input-group-addon">ต่อ <?=$farm["area"]?> ไร่</span>-->
        <span class="input-group-addon">บาท</span>
    </div>
</div>
<script type="text/javascript">
    function calculateSum(obj) {
        var sum = 0;
        //iterate through each textboxes and add the values
        $("."+obj).each(function() {
            //add only if the value is number
            if(!isNaN(this.value) && this.value.length!=0) {
                sum += parseFloat(this.value);
            }
 
        });
        //.toFixed() method will roundoff the final sum to 2 decimal places
        $("#"+obj).html(number_format(sum.toFixed(0)));
    }
</script>